unit hadedataset;
{$I ..\hddefines.inc}
{$mode objfpc}{$H+}

interface

uses
  Classes,
  SysUtils,
  hadebaseobject,
  hadebasedataset,
  hadeconnectionmanager;

type

  { THadeDataSet }

  THadeDataSet = class(THadeBaseObject)
  private
    FDataSet: IHadeDataSet;
  protected
    procedure SetSQL(AValue: TStrings);
    function GetSQL: TStrings;

    procedure SetValueByName(const AProp: string; const AVal: variant);
    function getValueByName(const AColName: string): variant;

    procedure setStringByName(const AProp: string; const AVal: string);
    function getStringByName(const AColName: string): string;

    function getIntegerByName(const AColName: string): integer;
    procedure setIntegerByName(const AProp: string; const AVal: integer);

    function getDoubleByName(const AColName: string): double;
    procedure setDoubleByName(const AProp: string; const AVal: double);

    function getDateTimeByName(const AColName: string): TDateTime;
    procedure setDateTimeByName(const AProp: string; const AVal: TDateTime);
  public
    function Count: integer;

    procedure Open;
    procedure Close;
    procedure ExecSQL;

    procedure Next;
    procedure First;
    procedure Last;
    procedure Prior;
    function EOF: boolean;

    property SQL: TStrings read GetSQL write SetSQL;

    property ValueByName[const AColName: string]: variant
      read getValueByName write setValueByName;
    property StringByName[const AColName: string]: string
      read getStringByName write setStringByName;
    property IntegerByName[const AColName: string]: integer
      read getIntegerByName write setIntegerByName;
    property DoubleByName[const AColName: string]: double
      read getDoubleByName write setDoubleByName;
    property DateTimeByName[const AColName: string]: TDateTime
      read getDateTimeByName write setDateTimeByName;

    function getLastInsertID(ATableName:String):int64;

    Procedure ExecuteMultiple(Asql:String);

    constructor Create(const AConnection: THadeConnection);
  end;

implementation

uses
  hadebroker,
  hadesqldbdataset{$IFDEF INDEBUG},dbugintf{$ENDIF}
  ;

{ THadeDataSet }

procedure THadeDataSet.SetSQL(AValue: TStrings);
begin
  FDataSet.SQL := AValue;
end;

function THadeDataSet.GetSQL: TStrings;
begin
  Result := FDataSet.SQL;
end;

procedure THadeDataSet.SetValueByName(const AProp: string; const AVal: variant);
begin
  FDataSet.ValueByName[AProp] := AVal;
end;

function THadeDataSet.getValueByName(const AColName: string): variant;
begin
  Result := FDataSet.ValueByName[AColName];
end;

procedure THadeDataSet.setStringByName(const AProp: string; const AVal: string);
begin
  FDataSet.StringByName[AProp] := AVal;
end;

function THadeDataSet.getStringByName(const AColName: string): string;
begin
  Result := FDataSet.StringByName[AColName];
end;

function THadeDataSet.getIntegerByName(const AColName: string): integer;
begin
  Result := FDataSet.IntegerByName[AColName];
end;

procedure THadeDataSet.setIntegerByName(const AProp: string; const AVal: integer);
begin
  FDataSet.IntegerByName[AProp] := AVal;
end;

function THadeDataSet.getDoubleByName(const AColName: string): double;
begin
  Result := FDataSet.DoubleByName[AColName];
end;

procedure THadeDataSet.setDoubleByName(const AProp: string; const AVal: double);
begin
  FDataSet.DoubleByName[AProp] := AVal;
end;

function THadeDataSet.getDateTimeByName(const AColName: string): TDateTime;
begin
  Result := FDataSet.DateTimeByName[AColName];
end;

procedure THadeDataSet.setDateTimeByName(const AProp: string; const AVal: TDateTime);
begin
  FDataSet.DateTimeByName[AProp] := AVal;
end;

function THadeDataSet.Count: integer;
begin
  Result := FDataSet.Count;
end;

procedure THadeDataSet.Open;
begin
  {$IFDEF INDEBUG}
  dbugintf.SendDebug(FDataset.SQL.Text);
  {$ENDIF}
  FDataSet.Open;
end;

procedure THadeDataSet.Close;
begin
  FDataSet.Close;
end;

procedure THadeDataSet.ExecSQL;
begin
  {$IFDEF INDEBUG}
  dbugintf.SendDebug(FDataset.SQL.Text);
  {$ENDIF}
  FDataSet.ExecSQL;
end;

procedure THadeDataSet.Next;
begin
  FDataSet.Next;
end;

procedure THadeDataSet.First;
begin
  FDataSet.First;
end;

procedure THadeDataSet.Last;
begin
  FDataSet.Last;
end;

procedure THadeDataSet.Prior;
begin
  FDataSet.Prior;
end;

function THadeDataSet.EOF: boolean;
begin
  Result := FDataSet.EOF;
end;

function THadeDataSet.getLastInsertID(ATableName: String): int64;
begin
  Result := FDataSet.getLastInsertID(ATableName);
end;

procedure THadeDataSet.ExecuteMultiple(Asql: String);
begin
  {$IFDEF INDEBUG}
  dbugintf.SendDebug(Asql);
  {$ENDIF}
  FDataSet.ExecuteMultiple(Asql);
end;

constructor THadeDataSet.Create(const AConnection: THadeConnection);
var
  broker: THdBroker;
begin
  broker := AConnection.Broker;
  if broker in THdSQLdbBroker then
  begin
    FDataSet := THadeSQLDBDataSet.Create(AConnection);
  end;
end;

end.

